package com.rcw.commonlib.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.rcw.commonlib.CustomViewPager;
import com.rcw.commonlib.FragmentAdapter;
import com.rcw.commonlib.R;
import com.rcw.popup.FilterBean;
import com.rcw.popup.SingleFilterBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * create an instance of this fragment.
 */
public class FilterFragment extends DialogFragment{

    private static final String CONTACT_DATA="CONTACT_DATA";
    private static final String SINGLE_DATA="SINGLE_DATA";
    private static final String MUL_DATA="MUL_DATA";

    private CustomViewPager mViewPager;
    private TextView tvDes;

    private Context context;
    private List<Fragment> fragments=new ArrayList<>();
    private Dialog dialog;
    private List<String> pList;
    private List<SingleFilterBean> singleData;
    private List<FilterBean> mulData;

    public FilterFragment() {
    }

    public static FilterFragment newInstance(List<String> pList, List<SingleFilterBean> singleData, List<FilterBean> mulData) {
        FilterFragment fragment = new FilterFragment();
        Bundle args = new Bundle();
        args.putStringArrayList(CONTACT_DATA, (ArrayList<String>) pList);
        args.putParcelableArrayList(SINGLE_DATA, (ArrayList<SingleFilterBean>) singleData);
        args.putParcelableArrayList(MUL_DATA, (ArrayList<FilterBean>) mulData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            pList = getArguments().getStringArrayList(CONTACT_DATA);
            singleData = getArguments().getParcelableArrayList(SINGLE_DATA);
            mulData = getArguments().getParcelableArrayList(MUL_DATA);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_filter, container, false);
        //注册订阅者
        EventBus.getDefault().register(this);
        mViewPager= (CustomViewPager) view.findViewById(R.id.view_pager);
        initView();
        setBottom();
        return view;
    }

    private void initView() {
        fragments.add(MulFilterFragment.newInstance(singleData,mulData));
        fragments.add(SingleFilterFragment.newInstance(pList));
        //设置viewpager的adapter
        mViewPager.setAdapter(new FragmentAdapter(getChildFragmentManager(), fragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(fragments.size());
        //取消滑动切换
        mViewPager.setCanScroll(false);
    }

    private void setBottom() {
        dialog = getDialog();
        Window window = dialog.getWindow();
        //显示在底部
        assert window != null;
        window.setGravity(Gravity.RIGHT);
        //宽度填充屏幕
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();

        //
        int screenWidth = getScreenWidth();
        lp.width = (int) (screenWidth * 0.85);
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    /**
     * 获取屏幕的宽度（单位：px）
     *
     * @return 屏幕宽px
     */
    public  int getScreenWidth() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();// 创建了一张白纸
        windowManager.getDefaultDisplay().getMetrics(dm);// 给白纸设置宽高
        return dm.widthPixels;
    }

    /**
     * 获取屏幕的高度（单位：px）
     *
     * @return 屏幕高px
     */
    public int getScreenHeight() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();// 创建了一张白纸
        windowManager.getDefaultDisplay().getMetrics(dm);// 给白纸设置宽高
        return dm.heightPixels;
    }

    // 普通事件的处理
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(EventMessage message) {
        if (message.getPosition()==-1){
            dialog.dismiss();
        }else {
            mViewPager.setCurrentItem(message.getPosition());
        }

    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this); //解除注册
        super.onDestroy();

    }
}
